package com.tan.kotlintest.pictureload

import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.LruCache
import android.view.View
import com.tan.kotlintest.R
import com.tan.kotlintest.base.BaseActivity
import com.tan.kotlintest.util.DevicesUtil
import com.tan.kotlintest.util.LogUtil
import kotlinx.android.synthetic.main.activity_test_picture_load_layout.*


/*
 * Copyright (C) 2018 重庆一聊高科技有限公司
 * 版权所有
 *
 * 功能描述:  测试图片三级缓存
 * 作者：txj
 * 创建时间：2018/12/4
 *
 * 修改人：
 * 修改描述：
 * 修改日期
 */
class TestPictureLoadActivity : BaseActivity() {

    private var memoryCache: LruCache<String, Bitmap>? = null

    override fun getContentView(): View? {
        return inflate(R.layout.activity_test_picture_load_layout)
    }

    override fun onContentAdded() {
        setTitleText("测试图片三级缓存")
        initStart()
    }

    /**
     * 初始化
     */
    private fun initStart() {
        // get system max memory;
        val maxMemory = (Runtime.getRuntime().maxMemory() / 1024).toInt()
        // set LruCache size;
        val cacheSize = maxMemory / 8
        memoryCache = object : LruCache<String, Bitmap>(cacheSize) {
            override fun sizeOf(uri: String, bitmap: Bitmap): Int {
                return bitmap.rowBytes * bitmap.height / 1024
            }
        }
        /*val options = BitmapFactory.Options()
        val resizeBitmap = BitmapFactory.decodeResource(resources, R.drawable.image_big, options)
        LogUtil.i("分配的byte count:  " + resizeBitmap.allocationByteCount)
        test_iv.setImageBitmap(resizeBitmap)*/

        val resizeBitmap = resizePictureFromResource(resources, R.drawable.image_big,
                DevicesUtil.dp2px(applicationContext, 100f),
                DevicesUtil.dp2px(applicationContext, 200f))
        LogUtil.i("分配的byte count one:  " + resizeBitmap.allocationByteCount)
        test_iv.setImageBitmap(resizeBitmap)
        memoryCache?.put("haha", resizeBitmap)

        val bitmap: Bitmap = memoryCache?.get("haha")!!
        test_one_iv.setImageBitmap(bitmap)
    }

    // resize the picture from resource;
    private fun resizePictureFromResource(res: Resources, resId: Int, reqWidth: Int, reqHeight: Int): Bitmap {
        val options = BitmapFactory.Options()
        options.inJustDecodeBounds = true
        BitmapFactory.decodeResource(res, resId, options)
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight)
        options.inJustDecodeBounds = false
        return BitmapFactory.decodeResource(res, resId, options)
    }

    // the algorithm for calculate the inSampleSize;
    private fun calculateInSampleSize(options: BitmapFactory.Options, reqWidth: Int, reqHeight: Int): Int {
        if (reqWidth == 0 || reqHeight == 0) return 1
        val height = options.outHeight
        val width = options.outWidth
        LogUtil.i("origin, width = $width , height = $height")
        var inSampleSize = 1
        if (height > reqHeight || width > reqWidth) {
            val halfWidth = width / 2
            val halfHeight = height / 2
            while (halfHeight / inSampleSize >= reqHeight && halfWidth / inSampleSize >= reqWidth) {
                inSampleSize *= 2
            }
        }
        LogUtil.i("inSampleSize = $inSampleSize")
        return inSampleSize
    }


}